package com.annhoa.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 反射工具
 * 
 * @author annhoa
 *
 */
public class ReflectHelper {

	/**
	 * 获取obj对象fieldName的Field
	 * 
	 * @param obj
	 * @param fieldName
	 * @return
	 */
	public static Field getFieldByFieldName(Object obj, String fieldName) {
		for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
				.getSuperclass()) {
			try {
				return superClass.getDeclaredField(fieldName);
			} catch (NoSuchFieldException e) {
			}
		}
		return null;
	}

	/**
	 * 获取obj对象fieldName的属性值
	 * 
	 * @param obj
	 * @param fieldName
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static Object getValueByFieldName(Object obj, String fieldName)
			throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
		Field field = getFieldByFieldName(obj, fieldName);
		Object value = null;
		if (field != null) {
			if (field.isAccessible()) {
				value = field.get(obj);
			} else {
				field.setAccessible(true);
				value = field.get(obj);
				field.setAccessible(false);
			}
		}
		return value;
	}

	/**
	 * 设置obj对象fieldName的属性值
	 * 
	 * @param obj
	 * @param fieldName
	 * @param value
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static void setValueByFieldName(Object obj, String fieldName, Object value)
			throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
		Field field = obj.getClass().getDeclaredField(fieldName);
		if (field.isAccessible()) {
			field.set(obj, value);
		} else {
			field.setAccessible(true);
			field.set(obj, value);
			field.setAccessible(false);
		}
	}

	/**
	 * 单个对象的所有键值
	 * 
	 * @param object 单个对象
	 * 
	 * @return Map<String, Object> map 所有 String键 Object值 ex
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Map<String, Object> getKeyAndValue(Object obj) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 得到类对象
		Class userCla = (Class) obj.getClass();
		/* 得到类中的所有属性集合 */
		Field[] fs = userCla.getDeclaredFields();
		for (int i = 0; i < fs.length; i++) {
			Field f = fs[i];
			f.setAccessible(true); // 设置些属性是可以访问的
			Object val = new Object();
			try {
				val = f.get(obj);
				// 得到此属性的值
				map.put(f.getName(), val);// 设置键值
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}

		}
		return map.isEmpty() ? null : (Map<String, Object>) map.get("map");
	}

	/**
	 * 单个对象的某个键的值
	 * 
	 * @param object 对象
	 * 
	 * @param key    键
	 * 
	 * @return Object 键在对象中所对应得值 没有查到时返回空字符串
	 */
	public static Object getValueByKey(Object obj, String key) {
		// 得到类对象
		Class userCla = (Class) obj.getClass();
		/* 得到类中的所有属性集合 */
		Field[] fs = userCla.getDeclaredFields();
		for (int i = 0; i < fs.length; i++) {
			Field f = fs[i];
			f.setAccessible(true); // 设置些属性是可以访问的
			try {

				if (f.getName().endsWith(key)) {
					// System.out.println("单个对象的某个键的值==反射==" + f.get(obj));
					return f.get(obj);
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		// 没有查到时返回空字符串
		return "";
	}

	/**
	 * 多个（列表）对象的所有键值
	 * 
	 * @param object
	 * @return
	 */
	public static List<Map<String, Object>> getKeysAndValues(List<Object> object) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (Object obj : object) {
			Class userCla;
			// 得到类对象
			userCla = (Class) obj.getClass();
			/* 得到类中的所有属性集合 */
			Field[] fs = userCla.getDeclaredFields();
			Map<String, Object> listChild = new HashMap<String, Object>();
			for (int i = 0; i < fs.length; i++) {
				Field f = fs[i];
				f.setAccessible(true); // 设置些属性是可以访问的
				Object val = new Object();
				try {
					val = f.get(obj);
					// 得到此属性的值
					listChild.put(f.getName(), val);// 设置键值
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
			list.add(listChild);// 将map加入到list集合中
		}
		System.out.println("多个（列表）对象的所有键值====" + list.toString());
		return list;
	}

	/**
	 * 多个（列表）对象的某个键的值
	 * 
	 * @param object
	 * @param key
	 * @return List<Object>
	 */
	public static List<Object> getValuesByKey(List<Object> object, String key) {
		List<Object> list = new ArrayList<Object>();
		for (Object obj : object) {
			// 得到类对象
			Class userCla = (Class) obj.getClass();
			/* 得到类中的所有属性集合 */
			Field[] fs = userCla.getDeclaredFields();
			for (int i = 0; i < fs.length; i++) {
				Field f = fs[i];
				f.setAccessible(true); // 设置些属性是可以访问的
				try {
					if (f.getName().endsWith(key)) {
						list.add(f.get(obj));
					}
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println("多个（列表）对象的某个键的值列表====" + list.toString());
		return list;
	}
}